var express = require('express'),
	app = express();
	server = require('http'),
	serverWeb = server.createServer(app),
	path = require('path'),
	config = require('./config')(),
	//mongoose
	mongoose = require('mongoose'),
	home = require('./apps/controllers/c_home'),
	chatEmbed = require('./apps/controllers/c_chatEmbed'),
	// template engine
	ect = require('ect'),
	ectRenderer = ect({ watch: true, root: path.join( __dirname + '/apps/views'), gzip: true}),
	// socket server
	socketIo = require('./app-socket').listen(app);

// set engine template
app.engine('.ect', ectRenderer.render);

// CONNECT DATABASE
mongoose.connect('mongodb://localhost/bigson');

// some environment variables
app.set('views', __dirname + '/apps/views');
app.use(express.favicon(__dirname + '/apps/public/img/favicon.ico'));
app.use(express.logger('dev'));
app.use(express.urlencoded());
// app.use(express.json());
// app.use(express.methodOverride());
app.use(express.cookieParser(config.CKsecret));
app.use(express.session({secret: config.SSsecret, key: config.SSkey}));
app.use(app.router);
app.use('/static', express.static(path.join(__dirname, '/apps/public')));

// ATTACH DB
var attachDB = function(req, res, next){
    req.db = mongoose;
    next();
};

// ROUTER
app.all('/home', attachDB, function(req, res) {
	home(req, res);
});
app.all('/chatEmbed', attachDB, function(req, res) {
	chatEmbed.run(req, res);
});

serverWeb.listen(config.webPort, function() {

});

console.log('run server');